const PersonalCircleSer = require('../../service/personalCircle/personalCircleSer');
const saveImg = require('../../utils/saveImg');
const saveVideo = require("../../utils/saveVideo");

class PersonalCircleCon {
    // 查询
    async inquirePersonalCircleList(ctx) {
        const jsonData = ctx.request.body.SearchData || {}
        const listQuery = ctx.request.body.ListQuery || {page: 1, limit: 10}
        const whereData = {}
        if (jsonData.date) {
            whereData.createdAt = jsonData.date
        }
        if (jsonData.id) {
            whereData.id = jsonData.id
        }
        try {
            const result = await PersonalCircleSer.inquirePersonalCircleList(listQuery, whereData)
            ctx.body = {
                code: '200',
                data: result
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }

    }

    //按照日期降序查询所以
    async inquirePersonalCircleDateList(ctx) {
        const listQuery = ctx.request.body.ListQuery || {page: 1, limit: 4}
        try {
            const result = await PersonalCircleSer.inquirePersonalCircleDateList(listQuery)
            ctx.body = {
                code: '200',
                data: result
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }


    // 添加
    async addPersonalCircle(ctx) {
        const text = ctx.request.body.text
        if (!text) {
            ctx.body = {
                code: 999,
                msg: '请输入内容'
            }
            return
        }
        const json = {circle_content: text}
        try {
            const data = await PersonalCircleSer.addPersonalCircle(json)
            if (data.dataValues && data.dataValues.id) {
                ctx.body = {
                    code: '200'
                }
            } else {
                ctx.app.emit('error', e.toString(), ctx)
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }

    // 添加图片
    async addPersonalCircleImg(ctx) {
        try {
            const imgFile = ctx.request.files.file
            // 保存图片
            const ImgUrl = saveImg(imgFile)
            ctx.body = {
                errno: 0, // 注意：值是数字，不能是字符串
                data: {
                    url: ImgUrl,
                }
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }

    }

    // 添加博客视频
    async addPersonalCircleVideo(ctx) {
        try {
            const videoFile = ctx.request.files.file
            // 保存图片
            const videoUrl = await saveVideo(videoFile).catch(e => {
                ctx.app.emit('error', e.toString(), ctx)
            })
            ctx.body = {
                errno: 0, // 注意：值是数字，不能是字符串
                data: {
                    url: videoUrl.split('public')[1].replace(/\\/g, '/'), // 图片 src ，必须
                }
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }

    async delPersonalCircle(ctx) {
        const id = ctx.request.body.id
        try {
            const data = await PersonalCircleSer.delPersonalCircle(id)
            if (data === 1) {
                ctx.body = {
                    code: '200'
                }
            } else {
                ctx.body = {
                    code: '999',
                    message: '删除失败'
                }
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)

        }
    }

    async updatePersonalCircle(ctx) {
        const id = ctx.request.body.id
        const text = ctx.request.body.text
        try {
            const data = await PersonalCircleSer.updatePersonalCircle(text, id)
            if (data[0] === 1) {
                ctx.body = {
                    code: '200'
                }
            } else {
                ctx.app.emit('error', e.toString(), ctx)
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }

    //修改状态
    async updatePersonalCircleStatus(ctx) {
        const id = ctx.request.body.id
        const state = ctx.request.body.status
        try {
            const data = await PersonalCircleSer.updatePersonalCircleStatus(state, id)
            if (data[0] === 1) {
                ctx.body = {
                    code: '200'
                }
            } else {
                ctx.app.emit('error', e.toString(), ctx)
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }
    }
}

module.exports = new PersonalCircleCon()
